package com.dyh.quickdemozwei.webdav;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.NTCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.FileEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;

import java.io.File;

/**
 * @Author: yhdong
 * @Date: 2021/11/29 10:24
 */
public class DyhWebdavUtil {

    /*
    https://dav.jianguoyun.com/dav/
    dyh  afwfkf33fdy5bdbd
    */

//    static  String host = "https://dav.jianguoyun.com/dav/";
    static  String host = "dav.jianguoyun.com";
   // static String user = "dyh";
    static String user = "937135141@qq.com";
    static String password = "afwfkf33fdy5bdbd";
    static String prefix = "https";

//    private static Integer prot = 80;
    private static Integer prot = 8001;

    public static void main(String[] args) throws Exception {
        String filePath = "D:\\neo-work\\quick-demo-zwei\\src\\main\\java\\com\\dyh\\quickdemozwei\\webdav\\p.json";
        upload("filePath","/dav/hello");
    }

    public static void upload(String sourcepath, String destpath) throws Exception {

        String path = host + destpath;

        CredentialsProvider credsProvider = new BasicCredentialsProvider();

        NTCredentials creds = new NTCredentials(user, password, "", "");

        credsProvider.setCredentials(new AuthScope(host, prot, null, "ntlm"), creds);

        CloseableHttpClient httpclient = HttpClients.custom()

                .setDefaultCredentialsProvider(credsProvider)

                .build();

        HttpHost target = new HttpHost(host, prot, prefix);

// 保证相同的内容来用于执行逻辑相关的请求

        HttpContext localContext = new BasicHttpContext();

        HttpPut httpPut = new HttpPut(path);

        File file = new File(sourcepath);

        HttpEntity entity = new FileEntity(file);

        httpPut.setEntity(entity);

        httpclient.execute(target, httpPut, localContext);

        httpclient.close();

    }
}
